<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Drink extends CPT_Controller
{
    function __construct()
    {
            parent::__construct();
    }
    
    /*
     * Fetches all drinks from the database and stores it in
     * the view data. This function is called if there is no 
     * other method called.
     */
    public function index(){
        $this->headline = 'Drinks';

        //get all drinks
        $this->load->model('drinkmodel');
        $this->load->model('categorymodel');
        
        $this->view_data['categories']  = $this->categorymodel->get_categories();
        $this->view_data['drinks']      = $this->drinkmodel->get_all_drinks();
    }
    
    /*
     * Fetches one drink and all its properties from the database
     */
    public function details($drink_id = FALSE){
        if(!$drink_id){
            redirect('drink');
        }
	
	//rating success/error messages
	if($this->input->get('err', TRUE))
	    $this->view_data['error'] = 'Your rating could not be processed!';
	if($this->input->get('succ', TRUE))
	    $this->view_data['success'] = 'Rating successfully applied!';
		
	//retrieve drink data
        $this->load->model('drinkmodel', 'drink');
        $this->load->model('usermodel', 'user');
        
        /**
         * @todo There are a whole universe of errors that may occur here,
         * we better do some kind of try{} catch{} stuff since I do not
         * want to go through logs for hours to find that one little
         * mistake we may have made here...
         */
	$this->view_data['details']     = $this->drink->get_details($drink_id);
        $this->view_data['ingredients'] = $this->drink->get_ingredients($drink_id);
        $this->view_data['categories']  = $this->drink->get_categories($drink_id);
        $this->view_data['rating']      = $this->drink->get_rating($drink_id);
        $this->view_data['rated']       = $this->user->get_rating($this->tank_auth->get_user_id(), $drink_id);
        
        //generate rating percentage
        $avg = $this->view_data['rating']->avg;
        $this->view_data['rating']->perc = $avg / 5 * 100;
        
        
        $this->headline = $this->view_data['drink']['details']->name;
    }
    
    /*
     * Stores a rating made by an user in the database and redirects
     * to the drink details page.
     * 
     */
    public function rate($drink_id = FALSE){
	//get uri strings from url, convert it to int type
	$user_id = intval($this->uri->segment(4));
	$mod	 = intval($this->uri->segment(5));
	
	
	//check wether everything is set correctly
	if(FALSE == $drink_id)
	    redirect('drink');
	elseif (FALSE == $user_id || FALSE === $mod)		    #we need an === here since $mod can be 0 which equals to FALSE
	    redirect('drink/details/' . $drink_id . '?err=true');
	elseif (1 !== $mod && 0 !== $mod)
	    redirect('drink/details/' . $drink_id . '?err=true');
	
	//try to find an existing rating record
	$user   = Model\User::find($user_id);			    #fetch current user
	$record = $user->get_drinkRating($drink_id);		    #get drink rating record
	
	if(FALSE == $record){					    #no record found
	    $record = new Model\Drink\Rating();			    #create new rating object
	    $record->drink_id = $drink_id;
	    $record->user_id  = $user_id;
	}
	
	$record->rating	= $mod * 5;
	$record->save();					    #saves the current object (e.g. does a db INSERT/UPDATE)
	
	redirect('drink/details/' . $drink_id . '?succ=true');
    }
    
    /*
     * Fetches all drinks that were inserted by the current user
     */
    public function own($user_id = FALSE){
	if(FALSE == $user_id)
	    redirect('drink');
	
	$this->headline = 'Recipes by: ' . $this->tank_auth->get_username();
	$this->view_data['drinks'] = Model\User::find($user_id)->drink();
	
    }
    
    /*
     * 
     */
    public function create($user_id = FALSE){
        if(FALSE == $user_id)
            $this->display_notification('Drink creation failed!', 'error', 'drink');
        
        $this->headline = 'Create a new drink';
        $this->view_data['user_id'] = $user_id;
    }
}
/* End of file start.php */
/* Location: ./application/controllers/welcome.php */